//
//  replayHNTableCell.m
//  ChatDemo-UI2.0
//
//  Created by zz on 16/7/16.
//  Copyright © 2016年 zz. All rights reserved.
//

#import "replayHNTableCell.h"

@implementation replayHNTableCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        
        _headView = [[XZHeaderImageView alloc] initWithFrame:CGRectMake(12, 12, 36, 36)];
        [self.contentView addSubview:_headView];
        
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_headView.frame) + 8, 15, 150, 13)];
        _nameLabel.font = [UIFont systemFontOfSize:14];
        _nameLabel.textColor = RGBCOLOR(0, 0, 0);
        [self.contentView addSubview:_nameLabel];
        
        _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_headView.frame) + 8, CGRectGetMaxY(_nameLabel.frame) + 7, SCREENWIDTH - 68, 0)];
        _contentLabel.font = [UIFont systemFontOfSize:15];
        _contentLabel.textColor = RGBCOLOR(59, 59, 59);
        _contentLabel.numberOfLines = 0;
        [self.contentView addSubview:_contentLabel];
        
        
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREENWIDTH - 162, 16, 150, 10)];
        _timeLabel.font = [UIFont systemFontOfSize:11];
        _timeLabel.textAlignment = NSTextAlignmentRight;
        _timeLabel.textColor = RGBCOLOR(151, 151, 151);
        [self.contentView addSubview:_timeLabel];
        
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = RGBCOLOR(199, 199, 204);
        [self.contentView addSubview:_lineView];
        
    }
    
    return self;
}

-(void)updateCellWithModel:(ReplyModel *)model{
    
    NSString *logo = model.logo;
    
    if (logo && ![logo hasPrefix:@"http"]) {
        
        logo = [REQUESTIMAGEURL stringByAppendingString:logo];
    }
    
    [self.headView.headImage sd_setImageWithURL:[NSURL URLWithString:logo]];
    
    NSString *name = model.username;
    
    if (name.length > 2) {
        
        name = [name substringFromIndex:name.length - 2];
    }
    
    _headView.namelabel.text = name;
    _headView.flag = model.logotype == 1?YES:NO;
    
    
    NSString *scontent;
    
    if (model.touserid) {
        
        scontent = [NSString stringWithFormat:@"回复:@%@  %@",model.tousername,model.scontent];
    }else
    {
        scontent = model.scontent;
    }
    
    //回复内容
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:scontent];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineSpacing:1];
    [attStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, scontent.length)];
    _contentLabel.attributedText = attStr;
    //[_contentLabel sizeToFit];
    
    CGRect rect = _contentLabel.frame;
    rect.size.height = [self resizeContentLabelHeightWithString:scontent andLineSpacing:1 andFont:[UIFont systemFontOfSize:15] andMaxWidth:SCREENWIDTH - 68];
    _contentLabel.frame = rect;
    
    
    //昵称
    _nameLabel.text = model.username;

    
    //时间
    NSString *timeStr = model.jointime;

    NSString *getStr = [Utils getDataStringWithoutSecondString:timeStr];
    
    _timeLabel.text = getStr;
    
    _lineView.frame = CGRectMake(56, CGRectGetMaxY(_contentLabel.frame) + 11.5, SCREENWIDTH - 56, 0.5);
}

-(void)updateOACellWithModel:(OAReplyModel *)model{
    
    NSString *logo = model.logo;
    
    if (logo && ![logo hasPrefix:@"http"]) {
        
        logo = [REQUESTIMAGEURL stringByAppendingString:logo];
    }
    
    [self.headView.headImage sd_setImageWithURL:[NSURL URLWithString:logo]];
    
    NSString *name = model.username;
    
    if (name.length > 2) {
        
        name = [name substringFromIndex:name.length - 2];
    }
    
    _headView.namelabel.text = name;
    _headView.flag = model.logotype == 1?YES:NO;
    
    NSString *scontent;
    if (model.touserid) {
        
        scontent = [NSString stringWithFormat:@"回复:@%@  %@",model.tousername,model.scontent];
    }else
    {
        scontent = model.scontent;
    }
    
    //回复内容
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:scontent];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineSpacing:1];
    [attStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, scontent.length)];
    _contentLabel.attributedText = attStr;
    //[_contentLabel sizeToFit];
    
    CGRect rect = _contentLabel.frame;
    rect.size.height = [self resizeContentLabelHeightWithString:scontent andLineSpacing:1 andFont:[UIFont systemFontOfSize:15] andMaxWidth:SCREENWIDTH - 68];
    _contentLabel.frame = rect;
    
    
    //昵称
    _nameLabel.text = model.username;
    
    
    
    //时间
    NSString *timeStr = model.jointime;

    NSString *getStr = [Utils getDataStringWithoutSecondString:timeStr];
    
    _timeLabel.text = getStr;
    
    _lineView.frame = CGRectMake(56, CGRectGetMaxY(_contentLabel.frame) + 11.5, SCREENWIDTH - 56, 0.5);
}


-(CGFloat)resizeContentLabelHeightWithString:(NSString *)str andLineSpacing:(CGFloat)space andFont:(UIFont *)font andMaxWidth:(CGFloat)maxWidth{
    
    NSStringDrawingOptions opt = NSStringDrawingUsesLineFragmentOrigin;
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineSpacing:space];
    
    NSDictionary *att = @{NSFontAttributeName: font,NSParagraphStyleAttributeName:style};
    
    CGSize maxSize = CGSizeMake(maxWidth, CGFLOAT_MAX);
    
    CGSize size = [str boundingRectWithSize:maxSize options:opt attributes:att context:nil].size;
    return size.height;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
